import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:demo/providers/counter_providers.dart';

void main() {
  group('Simple Riverpod Tests', () {
    late ProviderContainer container;

    setUp(() {
      container = ProviderContainer();
    });

    tearDown(() {
      container.dispose();
    });

    test('初始状态应该正确', () {
      final state = container.read(counterProvider);
      expect(state.count, 0);
      expect(state.username, 'Guest');
      expect(state.isLoading, false);
      expect(state.status, 'Zero');
    });

    test('increment应该增加计数器', () {
      final notifier = container.read(counterProvider.notifier);
      
      notifier.increment();
      final state = container.read(counterProvider);
      
      expect(state.count, 1);
      expect(state.status, 'Low');
    });

    test('reset应该重置计数器', () {
      final notifier = container.read(counterProvider.notifier);
      
      notifier.addMultiple(10);
      notifier.reset();
      
      final state = container.read(counterProvider);
      expect(state.count, 0);
      expect(state.status, 'Zero');
    });

    test('setUsername应该更新用户名', () {
      final notifier = container.read(counterProvider.notifier);
      
      notifier.setUsername('Alice');
      
      final state = container.read(counterProvider);
      expect(state.username, 'Alice');
    });

    test('派生Provider应该正确工作', () {
      final notifier = container.read(counterProvider.notifier);
      
      notifier.addMultiple(15);
      
      final count = container.read(counterValueProvider);
      final status = container.read(statusProvider);
      final isEven = container.read(isEvenProvider);
      
      expect(count, 15);
      expect(status, 'Medium');
      expect(isEven, false);
    });

    test('设置Provider应该正确工作', () {
      final settingsNotifier = container.read(settingsProvider.notifier);
      
      settingsNotifier.updateSetting('theme', 'dark');
      
      final settings = container.read(settingsProvider);
      final theme = container.read(themeProvider);
      
      expect(settings['theme'], 'dark');
      expect(theme, 'dark');
    });

    test('进度Provider应该正确计算', () {
      final notifier = container.read(counterProvider.notifier);
      
      notifier.setCount(50);
      
      final progressTo100 = container.read(progressTo100Provider);
      final progressTo500 = container.read(progressTo500Provider);
      
      expect(progressTo100, 0.5);
      expect(progressTo500, 0.1);
    });

    test('里程碑Provider应该正确工作', () {
      final notifier = container.read(counterProvider.notifier);
      
      notifier.setCount(5);
      final milestone = container.read(nextMilestoneProvider);
      expect(milestone, '5 to reach 10');
      
      notifier.setCount(1500);
      final milestone2 = container.read(nextMilestoneProvider);
      expect(milestone2, 'You\'ve reached all milestones!');
    });

    test('CounterState copyWith应该正确工作', () {
      const state = CounterState();
      final newState = state.copyWith(count: 5, username: 'Alice');
      
      expect(newState.count, 5);
      expect(newState.username, 'Alice');
      expect(newState.isLoading, false); // 未改变的值应该保持
      expect(newState.status, 'Zero'); // 未改变的值应该保持
    });

    test('状态相等性应该正确工作', () {
      const state1 = CounterState(count: 5, username: 'Alice');
      const state2 = CounterState(count: 5, username: 'Alice');
      const state3 = CounterState(count: 10, username: 'Alice');

      expect(state1, equals(state2));
      expect(state1, isNot(equals(state3)));
    });
  });
}
